<?
include_once 'CoreModel.php';
Class ReportModel extends CoreModel {
	public function __construct() {
		parent::__construct();
	}
	public function getSaleRevenueCount($saleID){
		if($saleID!=""){
			$this->db->where("PERNR",$saleID);
		}
		$this -> db -> from("salesrevenue");
		
		return $this -> db -> count_all_results();
	}
	public function getOrderStatusCount($flag){
		$this->db->select("*");
		/*
		if($flag!="A"){
			$this->db->where("SALES_ORDER_FLAG",$flag);
		}else{
			$this->db->where("SALES_ORDER_FLAG != ''","",false);
		}
		*/
		if($flag!="A"){
			$this->db->where("SALES_ORDER_FLAG",$flag);
		}else{
			$this->db->where_in("SALES_ORDER_FLAG",array("F","C","-"));
		}
		$this->db->group_by("ORDER_NO");
		$this->db->from("zsd_web_order");
		return $this -> db -> count_all_results();
		
	}
	public function getOrderStatus($flag,$start,$limit){
		$this->db->select("*");
		if($flag!="A"){
			$this->db->where("SALES_ORDER_FLAG",$flag);
		}else{
			$this->db->where("SALES_ORDER_FLAG != ''","",false);
		}
		$this->db->group_by("ORDER_NO");
		$this->db->limit($limit,$start);
		$query  = $this->db->get("zsd_web_order");
		if ($query -> num_rows() > 0) {
			return $query->result();
		} else {
			return false;
		}
	}
	public function getSaleRevenueData($saleID,$start,$limit){
		$this->db->select("*,SUM(NETWR) AS sNETWR,SUM(MWSBP) AS sMWSBP,SUM(PRICE) AS sPRICE");
		if($saleID!=""){
			$this->db->where("PERNR",$saleID);
		}
		$this->db->group_by("VBELN");
		$this->db->limit($limit,$start);
		$query = $this -> db -> get("salesrevenue");
		if ($query -> num_rows() > 0) {
			return $query->result();
		} else {
			return false;
		}
		
	}
	public function getSummaryOther($saleID,$groupBy,$type,$startDate,$endDate,$start,$limit){
			
		$w = "";
		if($groupBy!="MATNR"){
			//$w = ",MATNR";
		}
		$where = "";
		if($saleID!=""){
			$where = " AND PERNR  = '$saleID' ";
		}
		$period = "";
		if($type=="p"){
			$period = "  (FKDAT BETWEEN '$startDate-0' AND '$endDate-0')";
		}else{
			$period = "  FKDAT  = '$startDate-0'";
		}
		
		$sql = "SELECT $groupBy as Con,MONTH(FKDAT) AS M,YEAR(FKDAT) AS Y,MATNR,MATNR_T,POSNR,VRKME,SUM(NETWR) as SUMS FROM  salesrevenue WHERE $period $where  group by $groupBy ".$w."";
		$sql.= " Order By MONTH(FKDAT) ASC ,YEAR(FKDAT) ASC,$groupBy ASC";
		$query = $this -> db -> query($sql);
		if ($query -> num_rows() > 0) {
			return $query->result();
		} else {
			return false;
		}	
		
	}
	public function getASaleRevenueData($saleID,$docID){
		$this->db->select("*");
		if($saleID!=""){
			$this->db->where("PERNR",$saleID);
		}
		$this->db->where("VBELN",$docID);
		
		$query = $this -> db -> get("salesrevenue");
		if ($query -> num_rows() > 0) {
			return $query->result();
		} else {
			return false;
		}
		
	}
}
?>